package com.hoshiicloud.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.rbac.entity.SysOrganization;
import com.hoshiicloud.rbac.entity.SysRoles;
import com.hoshiicloud.rbac.entity.SysUsers;
import com.hoshiicloud.rbac.vo.SysUsersVO;
import com.hoshiicloud.user.mapper.rbac.SysUsersMapper;
import com.hoshiicloud.user.param.rbac.SysUsersQueryParam;
import com.hoshiicloud.user.service.ISaasManageService;
import com.hoshiicloud.user.service.rbac.ISysOrganizationService;
import com.hoshiicloud.user.service.rbac.ISysRolesService;
import com.hoshiicloud.user.service.rbac.ISysUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Saas服务实现
 *
 * @author chenjie
 * @date 2019/7/30-10:58
 * @email rogercj1@126.com
 */
@Service
@Slf4j
public class SaasManageServiceImpl extends ServiceImpl<SysUsersMapper, SysUsers> implements ISaasManageService {

    @Autowired
    private ISysOrganizationService sysOrganizationService;

    @Autowired
    private ISysUsersService sysUsersService;

    @Autowired
    private ISysRolesService sysRolesService;



    /**
     *
     * @Description: Saas系统管理后台 员工管理-->员工列表
     *
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Response getEmployeePage(IPage<SysUsers> page,SysUsersQueryParam param,Long userId){
        Map addMap = new HashMap<>();
        IPage<SysUsersVO> responseUser = sysUsersService.getAllEmployee(page, param);
        List<SysOrganization> organizationList = sysOrganizationService.list();
        Response<SysUsers> response = sysUsersService.getSysUserDetail(userId);
        SysUsers sysUsers = response.getData();

        Response<SysOrganization> orgResponse = sysOrganizationService.getDetail(String.valueOf(sysUsers.getDeptId()));

        SysOrganization sysOrganization = orgResponse.getData();

        addMap.put("sysUsersList",responseUser);
        addMap.put("sysOrganization",sysOrganization);
        addMap.put("organizationList",organizationList);
        return Response.data(addMap);
     }


    /**
     *
     * @Description: Saas系统管理后台 员工管理-->角色列表
     *
     * @param page
     * @param key
     * @return
     */
    @Override
    public Response getRoleList(IPage<SysUsers> page, String key){
        Map addMap = new HashMap<>();

        List<SysRoles> roleList = sysRolesService.getAllSaasRoleList(1);

        SysUsersQueryParam sysUsersQueryParam = new SysUsersQueryParam();
        sysUsersQueryParam.setPageNumber(String.valueOf(page.getCurrent()));
        sysUsersQueryParam.setPageSize(String.valueOf(page.getSize()));
        IPage<SysUsersVO> newPage = sysUsersService.getAllEmployee(page,sysUsersQueryParam);
        addMap.put("roleList",roleList);
        addMap.put("userList",newPage);
        return Response.data(addMap);
    }

    /**
     *
     * @Description: SAAS系统根据部门ID分页查询(包括子部门)员工列表
     *
     * @param page
     * @param key
     * @param deptId
     * @return
     *
     */
    @Override
    public Response getEmployeePageByDeptId(IPage<SysUsers> page, String key, Long deptId){
        Map addMap = new HashMap<>();
        SysUsersQueryParam sysUsersQueryParam = new SysUsersQueryParam();
        sysUsersQueryParam.setPageNumber(String.valueOf(page.getCurrent()));
        sysUsersQueryParam.setPageSize(String.valueOf(page.getSize()));
        sysUsersQueryParam.setDeptId(deptId);
        IPage<SysUsersVO> newPage = sysUsersService.getPageByDeptId(page, sysUsersQueryParam);
        addMap.put("userList",newPage);
        return Response.data(addMap);
    }

    /**
     *
     * @Description: SAAS系统根据角色名称分页查询员工列表
     *
     * @param page
     * @param key
     * @param roleName
     * @return
     *
     */
    @Override
    public Response getEmployeePageByRoleName(IPage<SysUsers> page, String key, String roleName){
        Map addMap = new HashMap<>();
        SysUsersQueryParam sysUsersQueryParam = new SysUsersQueryParam();
        sysUsersQueryParam.setPageNumber(String.valueOf(page.getCurrent()));
        sysUsersQueryParam.setPageSize(String.valueOf(page.getSize()));
        sysUsersQueryParam.setRoleName(roleName);
        IPage<SysUsersVO> newPage = sysUsersService.getPageByRoleName(page, sysUsersQueryParam);
        addMap.put("userList",newPage);
        return Response.data(addMap);
    }


}
